﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Steve.TestHelper;
using Moq;
using System.ComponentModel.Composition;
using System.IO;
using Steve.UI.Extensions;
using Steve.UI.Extensions.Html;

namespace Steve.UI.Tests
{
    [TestClass]
    public class HtmlContentDataHandler_Tests : ComposablePartTests
    {
        public override List<Type> GetTypesForCatalog()
        {
            var list = base.GetTypesForCatalog();

            list.Add(typeof(DefaultRewriter));

            return list;
        }

        [TestMethod]
        public void Simple_Link_replacement_test()
        {
            var original = new Uri("http://testdomain.com/testurl/");
            var expected = new Uri("http://testdomain.com/testurl2/");

            HttpContextExport.Request.Mock(mock => mock.Setup(req => req.Url).Returns(original));

            var originalHtml = "<a href=\"" + original.ToString() + "\">Some text</a>";
            var expectedHtml = "<a href=\"" + expected.ToString() + "\">Some text</a>";

            StubSiteExplorerRepository.AddSite("testdomain", expected.Authority);
            var url = StubSiteExplorerRepository.AddUrl("testdomain", "Test URL", expected.AbsolutePath);
            StubSiteExplorerRepository.AddRewrite(url.Id, "testdomain", original.AbsolutePath);

            var handler = Compose<HtmlContentDataHandler>();
            var actualHtml = handler.GetText(new MemoryStream(Encoding.UTF8.GetBytes(originalHtml)), false);

            Assert.AreEqual(expectedHtml, actualHtml);
        }
    }
}
